package com.wpx.fileSys.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;


public class Token {
	public String getToken() {
		String s=UUID.randomUUID().toString();
		String token=s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24); 
		return token;
	}
	public Boolean verifyToken(String username,String token) {
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Connection conn=JdbcUtils.getConnection();
		String sql="select userName from User where token=? and userName = ?";
		
		try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, token);
			pstm.setString(2, username);
			rs=pstm.executeQuery();
			
			if (rs.next()) {
				return true;
			}else {
				return false;
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error 查找失败");
			return false;
		}finally{
			JdbcUtils.close(rs, pstm, conn);
		}
	}
}
